home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / bfd.mips / host-aout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-23  |  8.2 KB  |  275 lines

  1. #if 0
  2. /* BFD backend for local host's a.out binaries
  3.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  4.    Written by Cygnus Support.  Probably John Gilmore's fault.
  5.  
  6. This file is part of BFD, the Binary File Descriptor library.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <ansidecl.h>
  23. #include <sysdep.h>
  24. #include "bfd.h"
  25. #include "libbfd.h"
  26.  
  27. #include <a.out.h>
  28. #include "libaout.h"           /* BFD a.out internal data structures */
  29.  
  30. #include "trad-core.h"        /* Traditional Unix core files */
  31.  
  32. void (*bfd_error_trap)();
  33.  
  34. static bfd_target *host_aout_callback ();
  35.  
  36. /*SUPPRESS558*/
  37. /*SUPPRESS529*/
  38.  
  39. bfd_target *
  40. host_aout_object_p (abfd)
  41.      bfd *abfd;
  42. {
  43.   unsigned char magicbuf[4];    /* Raw bytes of magic number from file */
  44.   unsigned long magic;        /* Swapped magic number */
  45.  
  46.   bfd_error = system_call_error;
  47.  
  48.   if (bfd_read ((PTR)magicbuf, 1, sizeof (magicbuf), abfd) !=
  49.       sizeof (magicbuf))
  50.     return 0;
  51.   magic = bfd_h_getlong (abfd, magicbuf);
  52.  
  53.   if (N_BADMAG (*((struct exec *) &magic))) return 0;
  54.  
  55.   return some_aout_object_p (abfd, host_aout_callback);
  56. }
  57.  
  58. /* Set parameters about this a.out file that are machine-dependent.
  59.    This routine is called from some_aout_object_p just before it returns.  */
  60.  
  61. static bfd_target *
  62. host_aout_callback (abfd)
  63.      bfd *abfd;
  64. {
  65.   struct exec *execp = exec_hdr (abfd);
  66.  
  67.   /* The virtual memory addresses of the sections */
  68.   obj_datasec (abfd)->vma = N_DATADDR(*execp);
  69.   obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
  70.   obj_textsec (abfd)->vma = N_TXTADDR(*execp);
  71.  
  72.   /* The file offsets of the sections */
  73.   obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
  74.   obj_datasec (abfd)->filepos = N_DATOFF(*execp);
  75.  
  76.   /* The file offsets of the relocation info */
  77.   obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
  78.   obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
  79.  
  80.   /* The file offsets of the string table and symbol table.  */
  81.   obj_str_filepos (abfd) = N_STROFF (*execp);
  82.   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
  83.  
  84. #ifdef HOST_MACHINE_ARCH
  85.   abfd->obj_arch = HOST_MACHINE_ARCH;
  86. #endif
  87. #ifdef HOST_MACHINE_MACHINE
  88.   abfd->obj_machine = HOST_MACHINE_MACHINE;
  89. #endif
  90.  
  91.   return abfd->xvec;
  92. }
  93.  
  94.  
  95. boolean
  96. host_aout_mkobject (abfd)
  97.      bfd *abfd;
  98. {
  99.   char *rawptr;
  100.  
  101.   bfd_error = system_call_error;
  102.  
  103.   /* Use an intermediate variable for clarity */
  104.   rawptr = bfd_zalloc (abfd, sizeof (struct aoutdata) + sizeof (struct exec));
  105.  
  106.   if (rawptr == NULL) {
  107.     bfd_error = no_memory;
  108.     return false;
  109.   }
  110.  
  111.   set_tdata (abfd, (struct aoutdata *) rawptr);
  112.   exec_hdr (abfd) = (struct exec *) (rawptr + sizeof (struct aoutdata));
  113.  
  114.   /* For simplicity's sake we just make all the sections right here. */
  115.  
  116.   obj_textsec (abfd) = (asection *)NULL;
  117.   obj_datasec (abfd) = (asection *)NULL;
  118.   obj_bsssec (abfd) = (asection *)NULL;
  119.   bfd_make_section (abfd, ".text");
  120.   bfd_make_section (abfd, ".data");
  121.   bfd_make_section (abfd, ".bss");
  122.  
  123.   return true;
  124. }
  125.  
  126. /* Write an object file in host a.out format.
  127.    Section contents have already been written.  We write the
  128.    file header, symbols, and relocation.  */
  129.  
  130. boolean
  131. host_aout_write_object_contents (abfd)
  132.      bfd *abfd;
  133. {
  134. /* This works because we are on the host system */
  135. #define    EXEC_BYTES_SIZE    sizeof (struct exec)
  136.   size_t data_pad = 0;
  137.   unsigned char exec_bytes[EXEC_BYTES_SIZE];
  138.   struct exec *execp = exec_hdr (abfd);
  139.  
  140.   execp->a_text = obj_textsec (abfd)->size;
  141.  
  142.  
  143.   N_SET_MAGIC (*execp, OMAGIC);
  144.   if (abfd->flags & D_PAGED) {
  145.     /* This is not strictly true, but will probably do for the default
  146.     case.  FIXME.  
  147.     */
  148.  
  149.     execp->a_text = obj_textsec (abfd)->size + EXEC_BYTES_SIZE;
  150.     N_SET_MAGIC (*execp, ZMAGIC);
  151.   } else if (abfd->flags & WP_TEXT) {
  152.     N_SET_MAGIC (*execp, NMAGIC);
  153.   }
  154.   N_SET_FLAGS (*execp, 0x1);    /* copied from ld.c; who the hell knows? */
  155. #ifndef PAGE_SIZE 
  156. #define PAGE_SIZE 2048
  157. #endif
  158.   if (abfd->flags & D_PAGED) 
  159.       {
  160.     data_pad = ((obj_datasec(abfd)->size + PAGE_SIZE -1)
  161.             & (- PAGE_SIZE)) - obj_datasec(abfd)->size;
  162.  
  163.     if (data_pad > obj_bsssec(abfd)->size)
  164.       execp->a_bss = 0;
  165.     else 
  166.       execp->a_bss = obj_bsssec(abfd)->size - data_pad;
  167.     execp->a_data = obj_datasec(abfd)->size + data_pad;
  168.  
  169.       }
  170.   else {
  171.     execp->a_data = obj_datasec (abfd)->size;
  172.     execp->a_bss = obj_bsssec (abfd)->size;
  173.   }
  174.  
  175.   execp->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
  176.   execp->a_entry = bfd_get_start_address (abfd);
  177.  
  178.   execp->a_trsize = ((obj_textsec (abfd)->reloc_count) *
  179.              obj_reloc_entry_size (abfd));
  180.                
  181.   execp->a_drsize = ((obj_datasec (abfd)->reloc_count) *
  182.              obj_reloc_entry_size (abfd));
  183.  
  184.   bfd_aout_swap_exec_header_out (abfd, execp, exec_bytes);
  185.  
  186.   bfd_seek (abfd, 0L, false);
  187.   bfd_write ((PTR) exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
  188.  
  189.   /* Now write out reloc info, followed by syms and strings */
  190.  
  191.   if (bfd_get_symcount (abfd) != 0) 
  192.     {
  193.       bfd_seek (abfd,
  194.         (long)(N_SYMOFF(*execp)), false);
  195.  
  196.       aout_write_syms (abfd);
  197.  
  198.       bfd_seek (abfd,    (long)(N_TRELOFF(*execp)), false);
  199.  
  200.       if (!aout_squirt_out_relocs (abfd, obj_textsec (abfd))) return false;
  201.       bfd_seek (abfd, (long)(N_DRELOFF(*execp)), false);
  202.  
  203.       if (!aout_squirt_out_relocs (abfd, obj_datasec (abfd))) return false;
  204.     }
  205.   return true;
  206. }
  207.  
  208. /* We use BFD generic archive files.  */
  209. #define    aout_openr_next_archived_file    bfd_generic_openr_next_archived_file
  210. #define    aout_generic_stat_arch_elt    bfd_generic_stat_arch_elt
  211. #define    aout_slurp_armap        bfd_slurp_bsd_armap
  212. #define    aout_slurp_extended_name_table    bfd_true
  213. #define    aout_write_armap        bsd_write_armap
  214. #define    aout_truncate_arname        bfd_bsd_truncate_arname
  215.  
  216. /* We use traditional Unix core file format.  */
  217. #define    aout_core_file_failing_command    trad_unix_core_file_failing_command
  218. #define    aout_core_file_failing_signal    trad_unix_core_file_failing_signal
  219. #define    aout_core_file_matches_executable_p    \
  220.                     trad_unix_core_file_matches_executable_p
  221.  
  222. /* We replace this function */
  223. #define    aout_write_object_contents    host_aout_write_object_contents
  224.  
  225. bfd_target host_aout_big_vec =
  226. {
  227.   "a.out-host-big",        /* name */
  228.   bfd_target_aout_flavour_enum,
  229.   true,                /* target byte order */
  230.   true,                /* target headers byte order */
  231.   (HAS_RELOC | EXEC_P |        /* object flags */
  232.    HAS_LINENO | HAS_DEBUG |
  233.    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  234.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  235.   ' ',                /* ar_pad_char */
  236.   16,                /* ar_max_namelen */
  237.   _do_getb64, _do_putblong, _do_getbshort, _do_putbshort, /* data */
  238.   _do_getb64, _do_putblong, _do_getbshort, _do_putbshort, /* hdrs */
  239.  
  240.     {_bfd_dummy_target, host_aout_object_p,
  241.        bfd_generic_archive_p, trad_unix_core_file_p},
  242.     {bfd_false, host_aout_mkobject,
  243.        _bfd_generic_mkarchive, bfd_false},
  244.     {bfd_false, aout_write_object_contents,    /* bfd_write_contents */
  245.        _bfd_write_archive_contents, bfd_false},
  246.  
  247.   JUMP_TABLE(aout)
  248. };
  249.  
  250. bfd_target host_aout_little_vec =
  251. {
  252.   "a.out-host-little",        /* name */
  253.   bfd_target_aout_flavour_enum,
  254.   false,            /* target byte order */
  255.   false,            /* target headers byte order */
  256.   (HAS_RELOC | EXEC_P |        /* object flags */
  257.    HAS_LINENO | HAS_DEBUG |
  258.    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  259.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  260.   ' ',                /* ar_pad_char */
  261.   16,                /* ar_max_namelen */
  262.   _do_getllong, _do_putllong, _do_getlshort, _do_putlshort, /* data */
  263.   _do_getllong, _do_putllong, _do_getlshort, _do_putlshort, /* hdrs */
  264.  
  265.     {_bfd_dummy_target, host_aout_object_p,
  266.        bfd_generic_archive_p, trad_unix_core_file_p},
  267.     {bfd_false, host_aout_mkobject,
  268.        _bfd_generic_mkarchive, bfd_false},
  269.     {bfd_false, aout_write_object_contents,    /* bfd_write_contents */
  270.        _bfd_write_archive_contents, bfd_false},
  271.  
  272.   JUMP_TABLE(aout)
  273. };
  274. #endif
  275.